/********************************************************************************
    Author :
********************************************************************************/

/********************************************************************************
    Include Files :
********************************************************************************/
#include "extern.h"
    #include "sdk_project_config.h"
    #include "Adc.h"
    #include "Pwm.h"
    #include "FTM.h"
    #include "Gpio.h"
	#include "Interrupt.h"
	#include "IoHwAb.h"
#include "alloc.h"
	#include "IoToButton.h"
	#include "IoToLight.h"
/********************************************************************************
    Defination :
********************************************************************************/

/********************************************************************************
    Comment : The unit brightness of light
********************************************************************************/
#define UNIT_BRIGHTNESS 1000
/********************************************************************************
    Comment : The mistake of Adc value
********************************************************************************/
#define MISTAKE 0.5

/********************************************************************************
    Type name :
********************************************************************************/

/********************************************************************************
    Comment : The description of type name
********************************************************************************/

/********************************************************************************
    Private variables :
********************************************************************************/

/********************************************************************************
    Comment : The description of private variables
********************************************************************************/

/********************************************************************************
    Private function declaration :
********************************************************************************/

/********************************************************************************
    Public function :
********************************************************************************/

/********************************************************************************
    Description :
        The description of this function
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidInitFunction(void)
{
    Gpio_vidInitMainFunction();

	Interrupt_vidInitFunction();

    Adc_vidInitFunction();

    Pwm_vidInitFunction();

    FTM_vidInitFunction();

}


/********************************************************************************
    Description :
        Update the brightness of light by updating PWM
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidUpdateBrightness(enuLightLevel enuLevel,enuLight enuLightIndex)
{
	switch(enuLightIndex)
	{
		case ENU_RED_LIGHT:
			Pwm_vidUpdateDuty(ENU_FTM0_CHANNEL0, enuLevel * UNIT_BRIGHTNESS);
			break;
		case ENU_GREEN_LIGHT:
			Pwm_vidUpdateDuty(ENU_FTM0_CHANNEL1, enuLevel * UNIT_BRIGHTNESS);
			break;
		case ENU_YELLOW_LOCK:
			Pwm_vidUpdateDuty(ENU_FTM2_CHANNEL1, enuLevel * UNIT_BRIGHTNESS);
			break;
	}
}

/********************************************************************************
    Description :
        Check button is pressed or not
********************************************************************************/
PUBLIC FUNC(boolean,IOHWAB_CODE) IoHwAb_bIsPress(enuButton enuButtonIndex)
{
	boolean bIsPress = FALSE;
	switch(enuButtonIndex)
	{
		case ENU_BUTTON_INDEX_1:
			bIsPress =  Gpio_bIsOn(ENU_READ_BUTTON_KEY1);
			break;
		case ENU_BUTTON_INDEX_2:
			bIsPress =  Gpio_bIsOn(ENU_READ_BUTTON_KEY2);
			break;
		case ENU_BUTTON_INDEX_3:
			bIsPress =  Gpio_bIsOn(ENU_READ_BUTTON_KEY3);
			break;
        default:
            break;
	}
	return bIsPress;
}

/********************************************************************************
    Description :
        Pull the select light up
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidPullUpPin(enuWriteLight enuPinType)
{
	switch(enuPinType)
	{
		case ENU_WRITE_SELECT_LIGHT:
			Gpio_vidPullUpPin(ENU_WRITE_SELECT_LED);
			break;
	}
}

/********************************************************************************
    Description :
        Pull the select light down
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidPullDownPin(enuWriteLight enuPinType)
{
	switch(enuPinType)
	{
		case ENU_WRITE_SELECT_LIGHT:
			Gpio_vidPullDownPin(ENU_WRITE_SELECT_LED);
			break;
	}
}

/********************************************************************************
    Description :
        Get the Adc value of red light
********************************************************************************/
PUBLIC FUNC(enuLightLevel,IOHWAB_CODE) IoHwAb_enuGetRedAdcValue(void)
{
	enuLightLevel enuLevel;
	enuLevel = (enuLightLevel)(Adc_ftGetChannel2Value() + MISTAKE);
	return enuLevel;
}

/********************************************************************************
    Description :
        Get the Adc value of green light
********************************************************************************/
PUBLIC FUNC(enuLightLevel,IOHWAB_CODE) IoHwAb_enuGetGreenAdcValue(void)
{
	enuLightLevel enuLevel;
	enuLevel = (enuLightLevel)(Adc_ftGetChannel3Value() + MISTAKE);
	return enuLevel;
}

/********************************************************************************
    Description :
        Check if there is interrupt about button1
********************************************************************************/
PUBLIC FUNC(boolean,IOHWAB_CODE) IoHwAb_bIsKey1Trigger()
{
	boolean bIsTrigger = FALSE;
	bIsTrigger = Interrupt_bGetKey1Trigger();
	if(bIsTrigger == TRUE)
	{
		Interrupt_vidClearKey1Trigger();
	}
	else
	{
		
	}
	
	return bIsTrigger;
}

/********************************************************************************
    Description :
        Check if there is interrupt about button2
********************************************************************************/
PUBLIC FUNC(boolean,IOHWAB_CODE) IoHwAb_bIsKey2Trigger()
{
	boolean bIsTrigger = FALSE;
	bIsTrigger = Interrupt_bGetKey2Trigger();
	if(bIsTrigger == TRUE)
	{
		Interrupt_vidClearKey2Trigger();
	}
	else
	{
		
	}
	
	return bIsTrigger;
}

/********************************************************************************
    Description :
        Check if 10ms timer is reached
********************************************************************************/
PUBLIC FUNC(boolean,IOHWAB_CODE) IoHwAb_b10msTimeout(void)
{
	boolean bTimeout = FALSE;
	bTimeout = FTM_b10msTimeout();
	return bTimeout;
}

/********************************************************************************
    Description :
        Check if 100ms timer is reached
********************************************************************************/
PUBLIC FUNC(boolean,IOHWAB_CODE) IoHwAb_b100msTimeout(void)
{
	boolean bTimeout = FALSE;
	bTimeout = FTM_b100msTimeout();
	return bTimeout;
}

/********************************************************************************
    Description :
        Clear the flag of 10ms timer
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidClear10msTimerFlag(void)
{
	FTM_vidClear10msTimerFlag();
}

/********************************************************************************
    Description :
        Clear the flag of 100ms timer
********************************************************************************/
PUBLIC FUNC(void,IOHWAB_CODE) IoHwAb_vidClear100msTimerFlag(void)
{
	FTM_vidClear100msTimerFlag();
}
/********************************************************************************
    Private function :
********************************************************************************/

/********************************************************************************
    Description :
        The description of this function
********************************************************************************/
